// $codepro.audit.disable numericLiterals, variableShouldBeFinal
/* Copyright (c) 2011 Team A CS2340. All rights reserved.*/

package edu.gatech.teama.doctorsoffice.client.view;

import javax.swing.JPanel;

import javax.swing.JLabel;

import com.thirdnf.ResourceScheduler.Scheduler;
import com.toedter.calendar.JDateChooser;

import edu.gatech.teama.doctorsoffice.client.presenter.AppointmentRequestPresenter;

import edu.gatech.teama.doctorsoffice. // $codepro.audit.disable spaceAroundPeriods
	client.view.scheduler.AppointmentRequestSchedulerModel;

import java.awt.BorderLayout;
import net.miginfocom.swing.MigLayout;

import javax.swing.JFrame;

import javax.swing.JButton;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.border.LineBorder;

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;

import java.awt.Color;
import java.util.Date;
import java.util.List;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JTextArea;

import javax.swing.JScrollPane;
import javax.swing.JComboBox;


/**
 * AppointmentRequestPanel.java
 * @author unknown
 * @version 1.0
 */
public class AppointmentRequestPanel extends JPanel {

	/** the presenter for this panel*/
	private final AppointmentRequestPresenter myPresenter;
	
	/** the list of available times*/
	private List<LocalTime> availableTimes;
	
	/**
	 * Create the panel.
	 */
	public AppointmentRequestPanel() { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		myPresenter = new AppointmentRequestPresenter();
		
		setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new MigLayout("", "[58px][grow][]", "[15px,grow][][]"));
		
		JLabel lblReason = new JLabel("Reason:");
		panel_1.add(lblReason, "cell 0 0,alignx left,aligny top");
		
		JButton btnOk = new JButton("OK");

		JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, "cell 1 0 1 3,grow");
		
		final JTextArea reasonText = new JTextArea();
		scrollPane.setViewportView(reasonText);
		
		panel_1.add(btnOk, "cell 2 0");
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainWindow.backToMainPanel();
			}
		});
		panel_1.add(btnCancel, "cell 2 1");
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		add(horizontalStrut, BorderLayout.WEST);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		add(horizontalStrut_1, BorderLayout.EAST);
		
		final JDateChooser dateChooser = new JDateChooser(new Date());
		final Scheduler scheduler = new Scheduler();
		scheduler.setModel(new AppointmentRequestSchedulerModel());
		scheduler.showDate(new LocalDate(dateChooser.getDate()));
		scheduler.setBorder(new LineBorder(new Color(0, 0, 0)));
		add(scheduler, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.NORTH);
		panel.setLayout(new MigLayout("", "[][269px,grow]", "[19px][]"));
		
		JLabel lblDate = new JLabel("Date:");
		panel.add(lblDate, "cell 0 0,alignx left,growy");

		final JComboBox availableTimesBox = new JComboBox();
		
		dateChooser.addPropertyChangeListener(new PropertyChangeListener() {
			
			@Override
			public void propertyChange(PropertyChangeEvent evt) {
				// TODO Auto-generated method stub
				LocalDateTime date = new LocalDateTime(dateChooser.getDate());
				scheduler.showDate(new LocalDate(date));
				availableTimes = myPresenter.getAvailableTimes(date);
				availableTimesBox.removeAllItems();
				for (LocalTime t : availableTimes) {
					availableTimesBox.addItem(t.toString("HH:mm"));
				}
				
			}
		});
		panel.add(dateChooser, "cell 1 0,grow");

		panel.add(availableTimesBox, "cell 1 1,growx");

		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) { // $codepro.audit.disable emptyMethod
				myPresenter.addAppointment(
						new LocalDate(dateChooser.getDate()), 
						availableTimes.get(availableTimesBox.getSelectedIndex()), 
						reasonText.getText());
				MainWindow.backToMainPanel();
			}
		});
	}
	
	/**
	 * main
	 * @param args
	 */
	public static void main(String[] args) {
		AppointmentRequestPanel myScheduler = new AppointmentRequestPanel();
		JFrame frame = new JFrame();
		frame.getContentPane().add(myScheduler);
		frame.pack();
		frame.setVisible(true);
	}

}
